<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 模拟tab产生空格
 * @param int $step
 * @param string $string
 * @param int $size
 * @return string
 */
function tab($step = 1, $string = ' ', $size = 4)
{
    return str_repeat($string, $size * $step);
}
//设置数控请求版本
function setGlobalsSysVersion($curVersion=''){
    if( '' == $curVersion ){
        return;
    }
    $sysVen = Config('sysTestVersion');
    if( isset($sysVen[$curVersion]) ) {
        $GLOBALS['SysDataBaseConfig'] = $sysVen[$curVersion]; //选择数据库
    }
}
function getUploadFileDir($d='images'){
    return (isset($GLOBALS['SysDataBaseConfig']) ? "/test/$d/":"/tmp/$d/");
}

//资源图片保存
function uploadWeiXinQRCode($fileData=''){
    $dir = getUploadFileDir();
    $return_dir = $dir.date('Ymd',time())."/";
    $new_dir = ROOT_PATH.'public'.$return_dir;
    if ( !is_dir($new_dir) ){ mkdir($new_dir); }
    $file_name = md5(time().rand(0,100)).".jpg";
    $new_file = $new_dir.$file_name;
    ob_start();
    ob_end_clean();
    $fp = fopen($new_file, "a");
    if (fwrite($fp, $fileData) === false) {
        return '';
    }
    fclose($fp);

    return $return_dir.$file_name;
}
//获取用户分享等级
function getShareLv($lv=0){
    $arr = ['','初级分享达人','中级分享达人','高级分享达人'];
    return isset($arr[$lv]) ? $arr[$lv] : $arr[0];
}
//图像域名
function getImgHost(){
    return 'https://share.94lang.com';
}

/**
 *转码silk
 **/
function get_tranSilk($old_url){
    //替换url
    $return_url = str_replace('silk','mp4',$old_url);
    $url = str_replace('https://share.94lang.com','public',$old_url);
    $url = ROOT_PATH.$url;

    //判断文件是否存在
    if(!is_file($url)){
        return ['code'=>1,'msg'=>'文件不存在','data'=>$url,'url'=>$url ];
    }

    $suffix = pathinfo($url);

    //判断格式是否正确
    if($suffix['extension'] != 'silk'){
        return ['code'=>1,'msg'=>'文件格式不正确','data'=>$old_url,'url'=>$return_url];
    }

    //判断该文件是否已经转码
    if(is_file($return_url)){
        return ['code'=>0,'data'=>$return_url,'msg'=>'','url'=>$return_url];
    }

    //转码文件
    $tran = tranSilk($url);
    return ['code'=>0,'data'=>$return_url,'msg'=>$tran,'url'=>$url];
}

/**
 *转码silk 为Mp3
 * @param string $url //文件地址
 * @return string 转码成功地址
 */
function tranSilk($url){
    $return = exec("/usr/local/ffpmg/silk-v3-decoder-master/converter.sh  ".$url."  mp4");
    return $return;
}

function show_sex($sex) {
    $arr = ['未知','男','女'];
    return isset($arr[$sex])?$arr[$sex]:'';
}